import { generalRequest } from "../services/genServ";
import {
    type DataResponse,
} from "../models/general";
import { useAppStore } from "../stores/app";
const appStore = useAppStore()

// 发布帖子后台数据请求方法
export function createPost(data: Object): Promise<DataResponse> {
    const res = generalRequest("/api/community/createPost", {
        method: 'POST',
        data
    });
    return res as Promise<DataResponse>;
}

// 加载帖子
export function loadPosts(skip: number, limit: number): Promise<DataResponse> {
    const res = generalRequest("/api/community/posts", {
        method: 'GET',
        params: { skip, limit }
    });
    return res as Promise<DataResponse>;
}

// 帖子详情
export function fetchPostDetails(postId: string): Promise<DataResponse> {
    return generalRequest(`/api/community/posts/${postId}`, {
        method: 'GET'
    }) as Promise<DataResponse>;
}

// 帖子评论详情
export function fetchPostComments(postId: string): Promise<DataResponse> {
    return generalRequest(`/api/community/comments/${postId}`, {
        method: 'GET'
    }) as Promise<DataResponse>;
}

// 发布评论后台数据请求方法
export function createComment(data: Object): Promise<DataResponse> {
    const res = generalRequest("/api/community/comments", {
        method: 'POST',
        data
    });
    return res as Promise<DataResponse>;
}

// 点赞帖子
export async function likePost(postId: string): Promise<DataResponse> {
    return generalRequest(`/api/community/posts/${postId}/like`, {
        method: 'POST'
    }) as Promise<DataResponse>;
}

// 获取帖子点赞状态
export async function getPostLikeStatus(postId: string): Promise<DataResponse> {
    return generalRequest(`/api/community/posts/${postId}/like-status`, {
        method: 'GET'
    }) as Promise<DataResponse>;
}

// 点赞评论
export async function likeComment(commentId: string): Promise<DataResponse> {
    return generalRequest(`/api/community/comments/${commentId}/like`, {
        method: 'POST'
    }) as Promise<DataResponse>;
}

// 获取评论点赞状态
export async function getCommentLikeStatus(commentId: string): Promise<DataResponse> {
    return generalRequest(`/api/community/comments/${commentId}/like-status`, {
        method: 'GET'
    }) as Promise<DataResponse>;
}

// 接口返回数据类型定义
export interface LikeResponse {
    liked: boolean;
    likes_count: number;
}

export async function loadAllPost():Promise<DataResponse>{
    return generalRequest('/api/community/getAllPosts',{
        method: 'GET'
    }) as Promise<DataResponse>;
}

export async function changePostStatus(postId:number,status:string):Promise<DataResponse>{
    return generalRequest('/api/community/changePostStatus',{
        method: 'POST',
        data:{
            postId:postId,
            status:status
        }
    }) as Promise<DataResponse>;
}
export async function getPostData():Promise<DataResponse>{
    return generalRequest('/api/community/getPostData',{
        method: 'GET'
    }) as Promise<DataResponse>;
}

